package com.demo1.controller.admin;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.demo1.entity.Admin;
import com.demo1.entity.R;
import com.demo1.service.IAdminService;
import com.demo1.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理员Controller
 */
@RestController
public class AdminController {

    @Autowired
    private IAdminService adminService;

    /**
     * 管理员登录
     */
    @PostMapping("/adminLogin")
    public R adminLogin(@RequestBody Admin admin){
        if(admin==null){
            R.error();
        }
        if(StringUtils.isEmpty(admin.getAdminName())){
            return R.error("用户名不能为空");
        }
        if(StringUtils.isEmpty(admin.getAdminPassword())){
            return R.error("密码不能为空");
        }
        Admin resultAdmin = adminService.getOne(new QueryWrapper<Admin>().eq("admin_name", admin.getAdminName()));
        if(resultAdmin==null){
            return R.error("管理员不存在");
        }
        if(!resultAdmin.getAdminPassword().trim().equals(admin.getAdminPassword())){
            return R.error("用户名或密码错误");
        }
        return R.ok("登录成功！");

    }

    //    修改密码
    @PostMapping("/admin/modifyPassword")
    public R modifyPassword(@RequestBody Admin admin){
        if(StringUtil.isEmpty(admin.getAdminName())){
            return R.error("用户名不能为空");
        }
        if(StringUtil.isEmpty(admin.getNewPassword())){
            return R.error("新密码不能为空");
        }
        adminService.update(new UpdateWrapper<Admin>().set("password",admin.getNewPassword()).eq("userName",admin.getAdminName()));
        return R.ok();
    }
}
